package com.rqf.mr.service.impl;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import oracle.jdbc.OracleTypes;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.rqf.mr.service.IOrderService;

@Service("OrderService")
public class OrderService implements IOrderService {

	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	private SimpleDateFormat time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 

	@Override
	public List<Map<String, Object>> getOrderListByInpatiIdCodename(
			String inpatiId, String codeName) {
		if ( StringUtils.isEmpty(inpatiId) || StringUtils.isEmpty(codeName) ) {
			return null;
		}
		String sql = "select * from view_ehis_order_history "
				+ "where INPATIID='"+inpatiId+"' and FLAG='"+codeName+"'";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
		for (int i = 0; i < list.size(); i++) {
			list.get(i).put("INPUTDATE", time.format((Date)list.get(i).get("INPUTDATE")));
		}
		return list;
	}

	@Override
	public List<Map<String, Object>> getOrderByInpatiId(String inpatiId) {
		if ( StringUtils.isEmpty(inpatiId) ) {
			return null;
		}
		String sql = "select * from view_ehis_orderplan veo "
				+ "where veo.INPATIID='" + inpatiId + "'";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
		for (int i = 0; i < list.size(); i++) {
			list.get(i).put("INPUTDATE", time.format((Date)list.get(i).get("INPUTDATE")));
		}
		return list;
	}

	@Override
	public List<Map<String, Object>> searchOrderHaveChecked(String inpatiid) {
		if ( StringUtils.isEmpty(inpatiid) ) {
			return null;
		}
		String sql = "select * from view_ehis_order_history dd "
				+ "where dd.Inpatiid='" + inpatiid + "' order by INPUTDATE";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
		for (int i = 0; i < list.size(); i++) {
			list.get(i).put("INPUTDATE", time.format((Date)list.get(i).get("INPUTDATE")));
		}
		return list;
	}

	@Override
	public List<Map<String, Object>> searchOrderByOrgid(String orgid) {
		if ( StringUtils.isEmpty(orgid) ) {
			return null;
		}
		String sql = "select veo.ORDDES,veo.INPATIID,veo.BEDDES,veo.SERIALNUMBER,veo.BEDID,veo.PATINAME  from view_ehis_orderplan veo "
				+ "where veo.orgid_loc="+orgid+" group by veo.ORDDES,veo.INPATIID,veo.BEDDES,veo.SERIALNUMBER,veo.BEDID,veo.PATINAME";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
		for (int i = 0; i < list.size(); i++) {
			list.get(i).put("INPUTDATE", time.format((Date)list.get(i).get("INPUTDATE")));
		}
		return list;
	}

	@Override
	public Boolean startOrder(String orgid, String nurseLoginId) {
		if ( StringUtils.isEmpty(nurseLoginId) || StringUtils.isEmpty(orgid)) {
			return null;
		}
		DataSource dataSource = jdbcTemplate.getDataSource();  
		Connection conn=null;  
		try {
			conn=dataSource.getConnection();
			CallableStatement cs = conn.prepareCall("{call PRO_UPDATE_BEGINNURSE(?,?)}");   
			cs.setString(1, nurseLoginId);  
			cs.setString(2, orgid); 
			cs.execute();  
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
		return true;
	}

	@Override
	public Boolean endOrder(String orgid, String nurseLoginId) {
		if ( StringUtils.isEmpty(nurseLoginId) || StringUtils.isEmpty(orgid)) {
			return null;
		}
		DataSource dataSource = jdbcTemplate.getDataSource();  
		Connection conn=null;  
		try {
			conn=dataSource.getConnection();
			CallableStatement cs = conn.prepareCall("{call PRO_UPDATE_ENDNURSE(?,?)}");   
			cs.setString(1, nurseLoginId);  
			cs.setString(2, orgid); 
			cs.execute();  
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}  
		return true;
	}

	@Override
	public Boolean stopOrder(String orgId) {
		if ( StringUtils.isEmpty(orgId)) {
			return null;
		}
		DataSource dataSource = jdbcTemplate.getDataSource();  
		Connection conn=null;  
		try {
			conn=dataSource.getConnection();
			CallableStatement cs = conn.prepareCall("{call PRO_UPDATE_BEGINNURSETONULL(?)}");   
			cs.setString(1, orgId);  
			cs.execute();  
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}  
		return true;
	}

	@Override
	public List<Map<String, Object>> getOrderInfoByOrderid(String orderId) {
		
		return null;
	}

}
